package com.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.base.comm.CommContent;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.PayinfoesService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Page;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_RechargeStatistics {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private PayinfoesService payinfoesService;

	@RequestMapping("/rechargeStatistics")
	public ModelAndView rechargeStatistics(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		// 登录的用户ID 以及操作权限
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		// 数据
		List<Subscribers> users = null;
		switch (adminUsergrade) {
		//  1. 判断是否是admin用户
		case 0:
			page.setCurrentPageNo(1);
			page.setRecordTotal(subscribersService.countByAll(null, null,null, null, null, null, null, null, null, null));
			users = subscribersService.selectByAll(page, null, null,null,null, null, null, null, null, null, null);
			break;
		//  2. 判断是否是OEM用户
		case 1:
			page.setCurrentPageNo(1);
			page.setRecordTotal(subscribersService.countByParentid(adminUserId, null, null,null, null, null, null, null, null, null, null));
			users = subscribersService.selectByParentid(page, adminUserId, null,null, null, null, null, null, null, null, null, null);
			break;
		//  2. 判断是否是商家用户
		case 2:
			page.setCurrentPageNo(1);
			page.setRecordTotal(subscribersService.countByShopidNotAll(adminUserId, null, null,null, null, null, null, null, null, null));
			users = subscribersService.selectByShopidNotAll(page, adminUserId, null, null, null,null, null, null, null, null, null);
			break;
		}
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if (users != null) {
			for (Subscribers u : users) {
				switch (adminUsergrade) {
				case 0:
					u.setRechargestatistics(payinfoesService.sumByMobile(u.getMobile()).getCardtype());
					break;
				case 1:
					u.setRechargestatistics(payinfoesService.sumByMobileAndParentid(u.getMobile(), u.getParentid()).getCardtype());
					break;
				case 2:
					u.setRechargestatistics(payinfoesService.sumByMobileAndShopid(u.getMobile(), adminUserId).getCardtype());
					break;
				}
				
				// 代理商名
				Users pUser = usersService.selectByPrimaryKey(u.getParentid());
				u.setParentid(pUser.getUsername());
				// 商家名
				Users sUser = usersService.selectByPrimaryKey(u.getShopid());
				u.setShopid(sUser.getUsername());
			}
			request.setAttribute("list", users);
			modelAndView.setViewName("jsp/index_RechargeStatistics");
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "基础用户充值信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/selectStatistics")
	public ModelAndView selectRecharge(HttpServletRequest request, String id, String username, Integer datastatus,
			Integer curPage, String shopid, String parentid, String startTimeFrom,
			 String startTimeTo, String endTimeFrom, String endTimeTo) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		if (username == null) {
			if (request.getSession().getAttribute("username") != null) {
				username = (String) request.getSession().getAttribute("username");
			}
		}
		if (shopid == null) {
			if (request.getSession().getAttribute("shopid") != null) {
				shopid = (String) request.getSession().getAttribute("shopid");
			}
		}
		if (parentid == null) {
			if (request.getSession().getAttribute("parentid") != null) {
				parentid = (String) request.getSession().getAttribute("parentid");
			}
		}
		if (datastatus == null) {
			if (request.getSession().getAttribute("datastatus") != null) {
				datastatus = (Integer) request.getSession().getAttribute("datastatus");
			} else {
				datastatus = 0;
			}
		}
		if(parentid.equals("undefined")) {
			parentid = null;
		}
		if(shopid.equals("undefined")) {
			shopid = null;
		}
		
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Subscribers> users = null;
		// choose = 2 默认分页查询
		Integer choose = 2;
		if (adminUsergrade < 2) {
			if (id != null && !id.equals("") || username != null && !username.equals("")
					|| shopid != null && !shopid.equals("") || parentid != null && !parentid.equals("")
					|| datastatus != 0 || startTimeFrom != null && !startTimeFrom.equals("")
					&& startTimeTo != null && !startTimeTo.equals("") || endTimeFrom != null 
					&& !endTimeFrom.equals("") && endTimeTo != null && !endTimeTo.equals("")) {
				choose = 1;
			}
		}

		if (adminUsergrade == 2) {
			if (id != null && !id.equals("") || username != null && !username.equals("") || datastatus != 0
				|| startTimeFrom != null && !startTimeFrom.equals("")&& startTimeTo != null 
				&& !startTimeTo.equals("") || endTimeFrom != null && !endTimeFrom.equals("") 
				&& endTimeTo != null && !endTimeTo.equals("")) {
				choose = 1;
			}
		}

		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		//oem查询或者商家查询
		String pName = null;
		String sName = null;
		String mobile = null;
		String sUserName = null;
		Boolean status = null;
		String sStartTimeFrom = null;
		String sStartTimeTo = null;
		String sEndTimeFrom = null;
		String sEndTimeTo = null;
//		Users us = null;
		Page page = new Page();
		if(adminUsergrade == 0 || adminUsergrade == 1) {
			if(parentid != null && !parentid.equals("") && !parentid.equals("undefined")) {
//				us =usersService.selectByUsername(parentid);
//				pName = us.getId();
				pName = parentid;
			}
			if(shopid != null && !shopid.equals("") && !shopid.equals("undefined")) {
//				us =usersService.selectByUsername(shopid);
//				sName = us.getId();
				sName = shopid;
			}
		}
		if(username !=null && !username.equals("")) {
			mobile = username;
		}
		if(startTimeFrom != null && !startTimeFrom.equals("") && startTimeTo != null && !startTimeTo.equals("")) {
			sStartTimeFrom = startTimeFrom;
			sStartTimeTo = startTimeTo;
		}
		if(endTimeFrom != null && !endTimeFrom.equals("") && endTimeTo != null && !endTimeTo.equals("")) {
			sEndTimeFrom = endTimeFrom;
			sEndTimeTo = endTimeTo;
		}
		if (datastatus != null) {
			// 判断当前可用性
			switch (datastatus) {
			case 2:
				status = false;
				break;
			case 1:
				status = true;
				break;
			case 0:
				break;
			}
		}
		page.setCurrentPageNo(curPage);
		if (adminUsergrade == 0) {
			page.setRecordTotal(subscribersService.countByAll(null, mobile, sUserName, pName ,sName,status,
					sStartTimeFrom, sStartTimeTo, sEndTimeFrom, sEndTimeTo));
			users = subscribersService.selectByAll(page, null, mobile, sUserName, pName ,sName, status,
					sStartTimeFrom, sStartTimeTo, sEndTimeFrom, sEndTimeTo);
		}
		
		if (adminUsergrade == 1) {
			page.setRecordTotal(subscribersService.countByParentid(adminUserId,null, mobile, sUserName, pName, sName, status,
					sStartTimeFrom, sStartTimeTo, sEndTimeFrom, sEndTimeTo));
			users = subscribersService.selectByParentid(page, adminUserId, null, mobile, sUserName, pName,sName,status,
					sStartTimeFrom, sStartTimeTo, sEndTimeFrom, sEndTimeTo);
		}
		
        if (adminUsergrade == 2) {
        	page.setRecordTotal(subscribersService.countByShopidNotAll(adminUserId,null, mobile, sUserName, sName, status,
        			sStartTimeFrom, sStartTimeTo, sEndTimeFrom, sEndTimeTo));
			users = subscribersService.selectByShopidNotAll(page, adminUserId,null, mobile, sUserName, sName, status,
					sStartTimeFrom, sStartTimeTo, sEndTimeFrom, sEndTimeTo);
		}
		if (choose == 2) {
			switch (adminUsergrade) {
			//  1. 判断是否是admin用户
			case 0:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(subscribersService.countByAll(null, null,null, null, null, null, null, null, null, null));
				users = subscribersService.selectByAll(page, null, null,null,null, null, null, null, null, null, null);
				break;
			//  2. 判断是否是OEM用户
			case 1:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(subscribersService.countByParentid(adminUserId, null,null,null, null, null, null, null, null, null, null));
				users = subscribersService.selectByParentid(page, adminUserId, null, null,null, null, null, null, null, null, null, null);
				break;
			//  2. 判断是否是商家用户
			case 2:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(subscribersService.countByShopidNotAll(adminUserId, null,null, null, null, null, null, null, null, null));
				users = subscribersService.selectByShopidNotAll(page, adminUserId, null,null, null, null, null, null, null, null, null);
				break;
			}
		}
		
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if (users != null) {
			for (Subscribers u : users) {
				switch (adminUsergrade) {
				case 0:
					u.setRechargestatistics(payinfoesService.sumByMobile(u.getMobile()).getCardtype());
					break;
				case 1:
					u.setRechargestatistics(payinfoesService.sumByMobileAndParentid(u.getMobile(), u.getParentid()).getCardtype());
					break;
				case 2:
					u.setRechargestatistics(payinfoesService.sumByMobileAndShopid(u.getMobile(), adminUserId).getCardtype());
					break;
				}
				
				// 代理商名
				Users pUser = usersService.selectByPrimaryKey(u.getParentid());
				u.setParentid(pUser.getUsername());
				// 商家名
				Users sUser = usersService.selectByPrimaryKey(u.getShopid());
				u.setShopid(sUser.getUsername());
			}
			
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/index_RechargeStatistics");
			request.setAttribute("username", username);
			request.setAttribute("parentid", parentid);
			request.setAttribute("shopid", shopid);
			request.setAttribute("list", users);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
